/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.getroot;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;


public class HelloJni extends AppCompatActivity
{
	 
	TextView mTv;
	TextView mTv2;
	
	static final int MSG_1 = 1;
	
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState)
    {
		setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		init();

    }

	private void init() {
		getSupportActionBar().setTitle("Root");

		mTv = (TextView) findViewById(R.id.tv);
		mTv2 = (TextView) findViewById(R.id.tv2);

		findViewById(R.id.check_root).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				RootChecker.checkIfRooted(HelloJni.this);

			}
		});
		findViewById(R.id.check_su).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				RootChecker.checkByExec();

			}
		});

		findViewById(R.id.exe_su).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				exec();

			}
		});
        exec();


	}

	private void exec(){
		BackgroundThread.getHandler().post(new Runnable() {

			@Override
			public void run() {
				exec(getApplicationContext());
			}

		});
	}

	private Handler mHandler = new Handler() {
    	@Override
    	public void handleMessage(Message msg) {
    		switch (msg.what) {
    		case MSG_1:
    			String r = (String) msg.obj;
    			mTv2.setText(r);
    			
    			String result = "--result--";
    			String tag = "!";
    			
    			int pos = r.indexOf(result);
    			if (pos != -1) {
    				int endPos = r.indexOf(tag, pos);
    				if (endPos != -1) {
    					String title = r.substring(pos, endPos);
    					mTv.setText(title);
    				}
    			}
    			
    			break;
    		}
    	}
    };
    
    
    private void exec(Context context) {
    	
    	long time = System.currentTimeMillis();
    	
    	RootHelper rh = new RootHelper(context);
//    	rh.prepare();
    	String r = rh.exePath("2");
    	
    	long elapse = System.currentTimeMillis() - time;    	
    	r = "elapse " + elapse + " ms,  " + r;
    	
    	Message msg = mHandler.obtainMessage(MSG_1, r);
    	mHandler.sendMessage(msg);
    }
    
//
//    /* A native method that is implemented by the
//     * 'hello-jni' native library, which is packaged
//     * with this application.
//     */
//    public native String  stringFromJNI();
//
//    /* This is another native method declaration that is *not*
//     * implemented by 'hello-jni'. This is simply to show that
//     * you can declare as many native methods in your Java code
//     * as you want, their implementation is searched in the
//     * currently loaded native libraries only the first time
//     * you call them.
//     *
//     * Trying to call this function will result in a
//     * java.lang.UnsatisfiedLinkError exception !
//     */
//    public native String  unimplementedStringFromJNI();
//
//    /* this is used to load the 'hello-jni' library on application
//     * startup. The library has already been unpacked into
//     * /data/data/com.example.HelloJni/lib/libhello-jni.so at
//     * installation time by the package manager.
//     */
//    static {
//        System.loadLibrary("hello-jni");
//    }
}
